import Vue from 'vue'
import App from './App.vue'
import ElementUi, { Loading } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from '../router/index'
import store from './store'
import http from 'axios'
import '../api/mock'

Vue.config.productionTip = false

Vue.use(ElementUi);
//axios是插件,使用的话要绑定在prototype
Vue.prototype.$http = http;
// Vue.prototype.axios = http;

router.beforeEach((to, from, next) => {
    store.commit('getToken');
    const token = store.state.user.token;
    if (!token && to.name != 'login') {
        next({ name: 'login' })
    } else if (token && to.name === 'login') {
        next({ name: 'home' })
    } else {
        next()
    }
})

//配置拦截器
let loadingInstance = null;
http.interceptors.request.use(config => {
    loadingInstance = Loading.service({ fullscreen: true })
        //配置token认证
    config.headers.Authorization = store.state.user.token;
    console.log(loadingInstance);
    console.log(config)
    return config
})

http.interceptors.response.use(function(response) {
    loadingInstance.close()
    return response
})

new Vue({
    store: store,
    router: router,
    a: 10,
    render: h => h(App),
    created() {
        store.commit('addMenu', router)
    }
}).$mount('#app')